package com.zbkj.crmeb.merchant.api.controller;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.constants.Constants;
import com.crmeb.core.common.CommonResult;
import com.factory.CommonFactory;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.finance.service.process.IRechargeOrderProcess;
import com.zbkj.crmeb.front.request.UserRechargeRequest;
import com.zbkj.crmeb.front.response.UserRechargePaymentResponse;
import com.zbkj.crmeb.front.response.UserRechargeResponse;
import com.zbkj.crmeb.front.service.UserCenterService;
import com.zbkj.crmeb.store.utilService.OrderUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * 用户 -- 充值
 */
@Slf4j
@RestController
@RequestMapping("api/front/mer/recharge")
@Api(tags = "用户 -- 充值")
public class FontUserRechargeController extends ApiBaseController {

    @Autowired
    private UserCenterService userCenterService;
    @Autowired
    private CommonFactory commonFactory;
    @Autowired
    private OrderUtils orderUtils;

    /**
     * 充值额度选择
     * @author Mr.Zhang
     * @since 2020-05-18
     */
    @ApiOperation(value = "充值额度选择")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public CommonResult<UserRechargeResponse> getRechargeConfig(HttpServletRequest request){
        return CommonResult.success(userCenterService.getRechargeConfig(getMerId(request)));
    }

    /**
     * 充值
     * @author Mr.Zhang
     * @since 2020-05-18
     */
    @ApiOperation(value = "小程序充值")
    @RequestMapping(value = "/routine", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> routineRecharge(HttpServletRequest httpServletRequest, @RequestBody UserRechargeRequest request) throws Exception {
    	
    	Integer merId = getMerId(httpServletRequest);

        // 获取支付渠道
        Integer type = orderUtils.getOrderPayType(merId).getCode();

        log.info("小程序充值: {}", JSON.toJSONString(request));
        request.setFromType(Constants.PAY_TYPE_WE_CHAT_FROM_PROGRAM);
        IRechargeOrderProcess process = commonFactory.getRechargeType(type);
        request.setMerId(merId);
        request.setClientIp(CrmebUtil.getClientIp(httpServletRequest));
        Map<String, Object> map = process.rechage(request);
        log.info("小程序充值返回: {}", JSON.toJSONString(map));
        
        return CommonResult.success(map);
    }

    /**
     * 充值
     * @author Mr.Zhang
     * @since 2020-05-18
     */
    @ApiOperation(value = "公众号充值")
    @RequestMapping(value = "/wechat", method = RequestMethod.POST)
    public CommonResult<UserRechargePaymentResponse> weChatRecharge(HttpServletRequest httpServletRequest, @RequestBody @Validated UserRechargeRequest request){
        request.setClientIp(CrmebUtil.getClientIp(httpServletRequest));
        return CommonResult.success(userCenterService.recharge(request));
    }

    /**
     * 佣金转入余额
     * @author Mr.Zhang
     * @since 2020-05-18
     */
    @ApiOperation(value = "余额转入")
    @RequestMapping(value = "/transferIn", method = RequestMethod.POST)
    public CommonResult<Boolean> transferIn(@RequestParam(name = "price") BigDecimal price){
        return CommonResult.success(userCenterService.transferIn(price));
    }
}



